package com.elinkway.infinitemovies.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.elinkway.infinitemovies.bean.SubscriptionAlbum;
import com.elinkway.infinitemovies.bean.SubscriptionVideo;
import com.elinkway.infinitemovies.utils.LogUtils;
import com.le123.ysdq.R;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * Created by zhaopeipei on 2016/9/6.
 */
public class SubscriptionContentListAdapter extends BaseAdapter {

    public Context mContext;

    public SubscriptionAlbum mData;

    private LayoutInflater inflater;

    private class ListItemHolder {
        ImageView mainIamge;
        TextView title;
        TextView duration;
    }

    public SubscriptionContentListAdapter(Context context, SubscriptionAlbum result) {
        super();
        mContext = context;
        inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mData = result;
    }

    @Override
    public int getCount() {
        return mData.getVideoList().size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup arg2) {
        ListItemHolder item;
        SubscriptionVideo subscriptionVideo = mData.getVideoList().get(position);
        if (convertView == null) {
            LogUtils.e("", "convert VIew");
            convertView = inflater.inflate(R.layout.subscription_content_item, null);
            item = new ListItemHolder();
            item.mainIamge = (ImageView) convertView.findViewById(R.id.iv_image);
            item.title = (TextView) convertView.findViewById(R.id.tv_content_title);
            item.duration = (TextView) convertView.findViewById(R.id.tv_play_time);
            convertView.setTag(item);
        } else {
            item = (ListItemHolder) convertView.getTag();
        }
        ImageLoader.getInstance().displayImage(subscriptionVideo.getImage(), item.mainIamge);
        item.title.setText(subscriptionVideo.getName());
        item.duration.setText(subscriptionVideo.getDuration());
        return convertView;
    }

    public void addItemData(SubscriptionAlbum moreData) {
        if (mData == null) {
            mData = moreData;
        } else {
            mData.getVideoList().addAll(moreData.getVideoList());
        }
    }

    public void setItemData(SubscriptionAlbum result) {
        mData = result;
    }

}
